import React from 'react';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {isYorN} from '../Enum';
import {isDestruction} from './constants';
import {TABLE, PAGINATION_OPTIONS, DATE_FORMAT, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT} from '../constants';

export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };

    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total} = this.props;
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.num',
                defaultMessage: '序号'
            }),
            dataIndex: 'num',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.region',
                defaultMessage: '区域'
            }),
            dataIndex: 'region',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.dealerCode',
                defaultMessage: '总代号'
            }),
            dataIndex: 'dealerCode',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.country',
                defaultMessage: '市场'
            }),
            dataIndex: 'country',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.usedPartTime',
                defaultMessage: '旧件保存期'
            }),
            dataIndex: 'usedPartTime',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.approveMonth',
                defaultMessage: '旧件已销毁到的月份'
            }),
            dataIndex: 'approveMonth',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.approveTime',
                defaultMessage: '销毁周期'
            }),
            dataIndex: 'approveTime',
            render: text => formatDateTime(text, DATE_FORMAT).join()
        }, {
            title: formatMessage({
                id: 'tablePanel.column.qty',
                defaultMessage: 'QTY'
            }),
            dataIndex: 'qty',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.code',
                defaultMessage: '旧件销毁清单号'
            }),
            dataIndex: 'code',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.destructionMethod',
                defaultMessage: '销毁方式'
            }),
            dataIndex: formatMessage({
                id: 'tablePanel.column.default',
                defaultMessage: '通知'
            })
        }, {
            title: formatMessage({
                id: 'tablePanel.column.submitTime',
                defaultMessage: '发布日期'
            }),
            dataIndex: 'submitTime',
            render: text => formatDateTime(text, DATE_FORMAT),
        }, {
            title: formatMessage({
                id: 'tablePanel.column.retrueTime',
                defaultMessage: '规定返回日期'
            }),
            dataIndex: 'retrueTime',
            render: text => formatDateTime(text, DATE_FORMAT),
        }, {
            title: formatMessage({
                id: 'tablePanel.column.approvePass',
                defaultMessage: '实际返回日期'
            }),
            dataIndex: 'approvePass',
            render: text => formatDateTime(text, DATE_FORMAT),
        }, {
            title: formatMessage({
                id: 'tablePanel.column.isOverdue',
                defaultMessage: '逾期Y/N'
            }),
            dataIndex: 'isOverdue',
            render: text => conventEnumValueToString(isYorN, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.currentTime',
                defaultMessage: '查询时间'
            }),
            dataIndex: 'currentTime',
            render: text => formatDateTime(text, DATETIME_FORMAT),
        }, {
            title: formatMessage({
                id: 'tablePanel.column.chargeCode',
                defaultMessage: '扣款单号'
            }),
            dataIndex: 'chargeCode',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.chargeMoney',
                defaultMessage: '扣款金额'
            }),
            dataIndex: 'chargeMoney',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.status',
                defaultMessage: '销毁状态'
            }),
            dataIndex: 'status',
            render: text => conventEnumValueToString(isDestruction, text)
        }];
        return (
            <Card>
                <Table
                    className={'white-space-nowrap'}
                    columns={columns}
                    dataSource={this.props.data}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    pagination={pagination}
                    rowKey="id"
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}
TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
import {injectIntl} from './intl';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));


